package com.atguigu.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.common.constant.AuthConstant;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.auth.feign.MemberFeifnService;
import com.atguigu.gulimall.auth.utils.HttpUtils;
import com.atguigu.common.vo.MemberEntityVo;
import com.atguigu.gulimall.auth.vo.SocialUserVo;
import com.atguigu.gulimall.auth.vo.UserVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: gulimall
 * @description:
 * @author: liuxvru
 * @create: 2024-09-06 21:05
 **/
@Slf4j
@Controller
@ConfigurationProperties(prefix = "oauth2")
@Data
public class OAuth2Controller {

    /**
     * clientId: 59e74fe039dd94a92d5d6b48ee741d5815a67d5b52c7cafa1ed29e6b8f83a5d1
     * redirectUri: http://auth.gulimall.com/success
     * clientSecret: a357463276e0cd97cec915f9076d32a3f5f10d51163b9dcaaf47c492557f66cd
     */
    private String host;
    private String userHost;
    private String path;
    private String userPath;
    private String method;
    private String userMethod;
    ;
    private String clientId;
    private String redirectUri;
    private String clientSecret;
    @Autowired
    private MemberFeifnService memberFeifnService;

    @GetMapping("/oauth2.0/gitee/sussess")
    public String login(@RequestParam("code") String code, HttpSession session) {
        log.debug("code:{}", code);

        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        Map<String, String> querys = new HashMap<>();
        querys.put("grant_type", "authorization_code");
        querys.put("code", code);
        querys.put("client_id", clientId);
        querys.put("redirect_uri", redirectUri);
        querys.put("client_secret", clientSecret);


        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, new HashMap<>(), querys, new HashMap<>());
            //获取response的body
            SocialUserVo socialUserVo = JSON.parseObject(EntityUtils.toString(response.getEntity()), SocialUserVo.class);
            Map<String, String> userQuerys = new HashMap<>();
            userQuerys.put("access_token", socialUserVo.getAccess_token());
            HttpResponse responseToken = HttpUtils.doGet(userHost, userPath, userMethod, new HashMap<>(), userQuerys);
            JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
            UserVo userVo = JSON.parseObject(EntityUtils.toString(responseToken.getEntity()), UserVo.class);
            log.debug(String.valueOf(userVo));
            R r = memberFeifnService.oauth2Login(userVo);
            if(r.getCode()==0){
                MemberEntityVo poJo = R.toPoJo(r.get("data"), MemberEntityVo.class);
                log.debug("登录成功{}",poJo);
                log.debug(session.getId());
                session.setAttribute(AuthConstant.LOGIN_USER,poJo);
                return "redirect:http://gulimall.com";
            }else{
                log.error("登录失败");
                return "redirect:http://auth.gulimall.com/login.html";
            }
        } catch (Exception e) {
            log.error("登录失败{}", e.toString());
            return "redirect:http://auth.gulimall.com/login.html";
        }


    }
}
